package com.qf.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import com.qf.pojo.Customer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lixu
 */
@RestController
@RequestMapping("/search")
public class SearchController {//当前服务不宕机的情况下
    /**
     * 模拟根据id查询
     * @param id
     */
/*    @GetMapping("/search/{id}")
    @HystrixCommand(fallbackMethod = "fallbackFindById",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "70"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "5000")
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "100"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "80"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "5000")
    })
    public Customer findById(@PathVariable Integer id) throws Exception {
        int i = 1/0;
        //System.out.println(Thread.currentThread().getName());
        //Thread.sleep(300);
        return new Customer(1,"张三",23);
    }*/
    /**
     * 使用熔断器进行降级处理
     */
    @GetMapping("/findById/{id}")
    //@HystrixCommand(fallbackMethod = "fallbackFindById")
    @HystrixCommand(fallbackMethod = "fallbackFindById",commandProperties = {
            //线程隔离策略 ： （熔断器线程池 THREAD) | ( tomcat的线程池 SEMAPHORE 信号量)
            @HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE"),//熔断器线程池
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000"),//超时时间也算失败
            @HystrixProperty(name = "execution.timeout.enabled",value = "true"),
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),//断路器的开关
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),//失败阈值的总请求数
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50"),//请求总数失败率达到%多少时
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "5000")//断路器open状态后，多少秒是拒绝请求的
    })
    public Customer findById(@PathVariable Integer id){ //Key(包名 + 类名 + 方法名 + 参数 )  value(Customer)
        System.out.println(Thread.currentThread().getName());
        //hystrix-SearchController-1
        // http-nio-9002-exec-4  tomcat
        if(id == 111){///112
            int i = 1/0;
        }
        System.out.println("=====搜索微服务被调用了、没有使用到缓存===");
        //80行
        return new Customer(id,"xixxii",20);
    }

    /**
     * 降级处理方法
     */
    public Customer fallbackFindById(Integer id){

        return new Customer(id,"我是降级处理方法的对象",20);
    }

}
